﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.Api.AuthenticationSchemeProviders;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.IdProviderStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits FluxorComponent
@inject Radzen.DialogService dialogService
@inject NotificationService notificationService
@inject IState<IdProviderDefsState> idProviderDefsState
@inject IDispatcher dispatcher

<RadzenSteps @bind-SelectedIndex="@selectedIndex">
    <Steps>
        <RadzenStepsItem Text="@Global.IdentityProviderType">
            <div class="row gy-4">
                @if (!idProviderDefsState.Value.IsLoading)
                {
                    @foreach(var idProviderDef in idProviderDefsState.Value.AuthProviderDefinitions)
                    {
                        <div class="col-md-6">
                            <RadzenCard @onclick="() => SelectIdProvider(idProviderDef)" class="@(SelectedIdProvider != null && SelectedIdProvider.Name == idProviderDef.Name ? "selected selectable" : "selectable")">
                                <div class="row">
                                    <div class="col-md-3">
                                        @if(string.IsNullOrWhiteSpace(idProviderDef.Image))
                                        {
                                            <RadzenImage Path="_content/SimpleIdServer.IdServer.Website/images/IdProvider.png" Style="width: 60px" />
                                        }
                                        else
                                        {
                                            <RadzenImage Path="@idProviderDef.Image" Style="width: 60px" />
                                        }
                                    </div>
                                    <div class="col">
                                        <h5>@idProviderDef.Name</h5>
                                        <p class="text-muted">@idProviderDef.Description</p>
                                    </div>
                                </div>
                            </RadzenCard>
                        </div>
                    }
                }
            </div>
        </RadzenStepsItem>
        <RadzenStepsItem Text="Details" Disabled="@(SelectedIdProvider == null)">
            <RadzenTemplateForm @ref="infoForm" TItem="IdProviderInfo" Data=@info>
                <!-- Name -->
                <div>
                    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Name</RadzenText>
                    <RadzenTextBox Name="Name" @bind-Value="@info.Name" Class="w-100"></RadzenTextBox>
                    <RadzenRequiredValidator Component="Name" Text="@Global.NameIsRequired"></RadzenRequiredValidator>
                </div>
                <!-- Display name -->
                <div>
                    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.DisplayName</RadzenText>
                    <RadzenTextBox Name="DisplayName" @bind-Value="@info.DisplayName" Class="w-100"></RadzenTextBox>
                    <RadzenRequiredValidator Component="DisplayName" Text="@Global.DisplayNameRequired"></RadzenRequiredValidator>
                </div>
                <!-- Description -->
                <div>
                    <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Description</RadzenText>
                    <RadzenTextArea @bind-Value="@info.Description" Class="w-100"></RadzenTextArea>
                </div>
            </RadzenTemplateForm>
        </RadzenStepsItem>
        <RadzenStepsItem Text="Properties" Disabled="@(SelectedIdProvider == null)">
            <EditConfiguration IsUpdating="@IsAdding" IsAddForm="true" PropertiesSaved=@SaveIdProvider Name=@SelectedIdProvider.OptionsName></EditConfiguration>
        </RadzenStepsItem>
    </Steps>
</RadzenSteps>

@code {
    int _selectedIndex;
    RadzenTemplateForm<IdProviderInfo> infoForm;
    record IdProviderInfo
    {
        public string Name { get; set; } = null!;
        public string DisplayName { get; set; } = null!;
        public string? Description { get; set; } = null;
        public string Source { get; set; }
    }
    bool IsAdding { get; set; } = false;
    IdProviderInfo info = new IdProviderInfo();
    EditContext editContext;
    AuthenticationSchemeProviderDefinition? SelectedIdProvider { get; set; } = null;
    int selectedIndex
    {
        get
        {
            return _selectedIndex;
        }
        set
        {
            if(value == 2)
            {
                if (!this.infoForm.EditContext.Validate()) return;
            }

            _selectedIndex = value;
        }
    }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        editContext = new EditContext(info);
        if(firstRender)
        {
            var act = new GetIdProviderDefsAction();
            dispatcher.Dispatch(act);
            SubscribeToAction<AddIdProviderSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.IdProviderAdded });
                this.IsAdding = false;
                dialogService.Close();
            });
            SubscribeToAction<AddIdProviderFailureAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
                this.IsAdding = false;
                StateHasChanged();
            });
        }
    }

    void SelectIdProvider(AuthenticationSchemeProviderDefinition idProvider)
    {
        SelectedIdProvider = idProvider;
    }

    void SaveIdProvider(Dictionary<string, string> properties)
    {
        this.IsAdding = true;
        var act = new AddIdProviderAction
        {
            Name = info.Name,
            IdProviderTypeName = SelectedIdProvider.Name,
            DisplayName = info.DisplayName,
            Description = info.Description,
            Properties = properties 
        };
        dispatcher.Dispatch(act);
    }
}